package com.wg.elasticsearch.result;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.wg.elasticsearch.enums.CodeEnum;
import com.wg.elasticsearch.util.StringUtil;

public class ResponseData {

    private static final long serialVersionUID = 1L;

    // 状态:true/false
    private boolean success = true;

    // 业务状态码
    private Integer code = CodeEnum.SUCCESS.getCode();

    // 信息
    private String msg = "";

    // 数据
    private Object data = null;

    /**
     * 失败,无信息返回
     *
     * @return
     */
    public static String toFail() {
        ResponseData response = new ResponseData(false, CodeEnum.FAIL.getCode());
        return response.toJson();
    }

    /**
     * 失败,并返回错误信息
     *
     * @param msg 错误信息
     * @return
     */
    public static String toFail(String msg) {
        ResponseData response = new ResponseData(false, CodeEnum.FAIL.getCode(), msg);
        return response.toJson();
    }

    /**
     * 失败,并返回错误信息和错误码\
     *
     * @param code 错误码
     * @param msg  错误信息
     * @return
     */
    public static String toFail(Integer code, String msg) {
        if(code == null) {
            code = CodeEnum.FAIL.getCode();
        }
        ResponseData response = new ResponseData(false, code, msg);
        return response.toJson();
    }

    /**
     * 失败,并返回错误信息和错误码
     *
     * @param msg  错误信息
     * @param data 错误码
     * @return
     */
    public static String toFail(String msg, Object data) {
        ResponseData response = new ResponseData(false, CodeEnum.FAIL.getCode(), msg, data);
        return response.toJson();
    }


    /**
     * 失败,并返回错误信息和错误码\
     *
     * @param code 错误码
     * @param msg  错误信息
     * @return
     */
    public static String toFail(Integer code, String msg, Object data) {
        if(code == null) {
            code = CodeEnum.FAIL.getCode();
        }
        ResponseData response = new ResponseData(false, code, msg, data);
        return response.toJson();
    }

    /**
     * 失败,参数不合法
     *
     * @return
     */
    public static String toParamInvalidateJson(String msg) {
        ResponseData response;
        if (StringUtil.isBlank(msg)) {
            response = new ResponseData(false,
                    CodeEnum.PARAM_INVALIDATE.getCode(), CodeEnum.PARAM_INVALIDATE.getMsg());
        } else {
            response = new ResponseData(false, CodeEnum.PARAM_INVALIDATE.getCode(), msg);
        }
        return response.toJson();
    }

    /**
     * 成功无信息返回
     *
     * @return
     */
    public static String toSuccess() {
        ResponseData response = new ResponseData(true, CodeEnum.SUCCESS.getCode());
        return response.toJson();
    }

    /**
     * 成功,并放回成功信息
     *
     * @param data 成功数据
     * @return
     */
    public static String toSuccess(Object data) {
        ResponseData response = new ResponseData(true, CodeEnum.SUCCESS.getCode(), CodeEnum.SUCCESS.getMsg(), data);
        return response.toJson();

    }

    /**
     * 成功,并返回数据
     *
     * @param msg  成功信息
     * @param data 成功数据
     * @return
     */
    public static String toSuccess(String msg, Object data) {
        ResponseData response = new ResponseData(true, CodeEnum.SUCCESS.getCode(), msg, data);
        return response.toJson();

    }

    public static String toSuccess(Integer code, String msg) {
        if(code == null) {
            code = CodeEnum.SUCCESS.getCode();
        }
        return new ResponseData(true, code, msg).toJson();
    }

    public static String toSuccess(Integer code, String msg, Object obj) {
        if(code == null) {
            code = CodeEnum.SUCCESS.getCode();
        }
        return new ResponseData(true, code, msg, obj).toJson();
    }

    /**
     * 请求成功，返回消息无数据返回
     * @param msg 返回信息
     * @return
     */
    public static String toSuccessMsg(String msg){
        ResponseData responseData = new ResponseData(true, CodeEnum.SUCCESS.getCode(), msg);
        return responseData.toJson();
    }

    public String toJson() {
        return JSONObject.toJSONString(this, SerializerFeature.BrowserCompatible
                , SerializerFeature.DisableCircularReferenceDetect);
    }



    public ResponseData() {

    }

    public ResponseData(boolean success, int code) {
        this.success = success;
        this.code = code;
    }

    public ResponseData(boolean success, int code, String msg) {
        this.success = success;
        this.code = code;
        this.msg = msg;
    }

    public ResponseData(boolean success, int code, String msg, Object data) {
        this.success = success;
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

}
